package net.vanchu.mortgagecalculator.common.config;

import java.text.DecimalFormat;
import java.util.Locale;

/**
 * Created by Granton on 2016/12/15.
 */
public class TestConfigProvider implements ConfigProvider {

    private static final String[] LOAN_NAMES = new String[]{
            "2015年10月24日基准利率",
            "2015年8月26日基准利率",
            "2015年6月27日基准利率",
            "2015年5月10日基准利率",
            "2015年3月01日基准利率",
            "2014年11月22日基准利率",
            "2012年7月6日基准利率",
            "2012年6月8日基准利率",
            "2011年7月7号基准利率",
            "2011年4月6号基准利率",
            "2011年2月9号基准利率"
    };

    private static final float[][] rates = new float[][]{
            new float[]{4.35f, 4.75f, 4.75f, 4.90f, 2.75f, 3.25f},
            new float[]{4.60f, 5.00f, 5.00f, 5.15f, 2.75f, 3.25f},
            new float[]{4.85f, 5.25f, 5.25f, 5.40f, 3.00f, 3.50f},
            new float[]{5.10f, 5.50f, 5.50f, 5.65f, 3.25f, 3.75f},
            new float[]{5.35f, 5.75f, 5.75f, 5.90f, 3.50f, 4.00f},
            new float[]{5.60f, 6.00f, 6.00f, 6.15f, 3.75f, 4.25f},
            new float[]{6.00f, 6.15f, 6.40f, 6.55f, 4.00f, 4.50f},
            new float[]{6.31f, 6.40f, 6.65f, 6.80f, 4.20f, 4.70f},
            new float[]{6.56f, 6.65f, 6.90f, 7.05f, 4.45f, 4.90f},
            new float[]{6.31f, 6.40f, 6.65f, 6.80f, 4.20f, 4.70f},
            new float[]{6.06f, 6.10f, 6.45f, 6.60f, 4.00f, 4.50f},
    };

    @Override
    public LoanRate[] provideProLoanRates() {
        int length = LOAN_NAMES.length;
        LoanRate[] loanRates = new LoanRate[length];
        for (int i = 0; i < length; i++) {
            loanRates[i] = new LoanRate(
                    LOAN_NAMES[i],
                    rates[i][0], rates[i][1], rates[i][2],
                    rates[i][3], rates[i][4], rates[i][5]
            );
        }
        return loanRates;
    }

    @Override
    public String[] provideLoansRateNames() {
        String[] result = new String[LOAN_NAMES.length];
        System.arraycopy(LOAN_NAMES, 0, result, 0, LOAN_NAMES.length);
        return result;
    }

    @Override
    public String[] provideDiscountNames() {
        DecimalFormat formatToTwoDecimal = new DecimalFormat("#.##");
        float[] discountValues = provideDiscountValues();
        int length = discountValues.length;
        String[] disCountNames = new String[length];
        for (int i = 0; i < length; i++) {
            disCountNames[i] = extractDisCountName(formatToTwoDecimal, discountValues[i]);
        }
        return disCountNames;
    }

    @Override
    public float[] provideDiscountValues() {
        float[] discounts = new float[34];
        discounts[0] = 1.2f;
        discounts[1] = 1.1f;
        discounts[2] = 1.05f;
        for (int i = 0; i < 31; i++) {
            discounts[3 + i] = (float) (1 - 0.01 * i);
        }
        return discounts;
    }

    @Override
    public String[] provideLoansWayNames() {
        return new String[]{"商业贷款", "公积金贷款", "组合贷款"};
    }

    @Override
    public LoanWay[] provideLoanWays() {
        return new LoanWay[]{
                LoanWay.wayCommercial,
                LoanWay.wayProvidentFund,
                LoanWay.wayMix};
    }

    public static String extractDisCountName(DecimalFormat formatToTwoDecimal, float discountValue) {
        if (discountValue < 1) {
            return String.format(Locale.getDefault(),
                    "%s折", formatToTwoDecimal.format(discountValue * 10));
        }
        if (discountValue > 1) {
            return String.format(Locale.getDefault(),
                    "%s倍", formatToTwoDecimal.format(discountValue));
        }
        return "不打折";
    }
}